/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fanta.framework.dao;

import fanta.framework.manager.ConnectionManager;
import fanta.framework.persistence.DateGiornate;
import fanta.framework.persistence.Formazione;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import fanta.framework.persistence.Giornatelega;
import fanta.framework.persistence.Lega;
import fanta.framework.persistence.Modulo;
import fanta.framework.persistence.Team;

/**
 *
 * @author alessandro
 */
public class GiornateLegaDAO {
    private static final Logger logger = Logger.getLogger(GiornateLegaDAO.class);

    public static Giornatelega getPartita(int id, ConnectionManager cm) {
        Giornatelega gl = new Giornatelega();
        try {
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
            Object obj = entityManager.find(Giornatelega.class, id);
            if(obj != null) {
                gl = (Giornatelega)obj;
            }
        }catch (Exception e) {
            logger.error("#Exception!# Errore in getPartita", e);
        }
        return gl;
    }
    public static Collection getPartite(ConnectionManager cm) {
        Collection listaPartite = new ArrayList();
        try {
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
            listaPartite = entityManager.createNamedQuery("Giornatelega.findAll").getResultList();
        } catch (Exception e) {
            logger.error("#EXCEPTION!# in getPartite", e);
        }
        return listaPartite;
    }
    public static Giornatelega creaPartita(DateGiornate data, Lega lega, Team team1, Team team2, double pti1, double pti2, Modulo modulo1,
            Modulo modulo2, Date dataapertura, Date datachiusura, Formazione Form1, Formazione Form2,
            ConnectionManager cm) {

        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        Giornatelega gl = new Giornatelega();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            gl.setData(data);
            gl.setDataapertura(dataapertura);
            gl.setDatachiusura(datachiusura);
            Collection formazioniC = new ArrayList<Formazione>();
            formazioniC.add(Form1);
            formazioniC.add(Form2);
            gl.setFormazioneCollection(formazioniC);
            gl.setIdteam1(team1);
            gl.setIdteam2(team2);
            gl.setLega(lega);
            gl.setModuloteam1(modulo1);
            gl.setModuloteam2(modulo2);
            gl.setPuntiteam1(pti1);
            gl.setPuntiteam2(pti2);
            entityManager.persist(gl);
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            logger.info("New giornatalega: "+gl);

        } catch (Exception e) {
            logger.error("Errore", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return gl;
    }
    public static boolean aggiornaGiornataLega(Giornatelega gl, ConnectionManager cm) {
        boolean result = false;
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.persist(entityManager.merge(gl));
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in aggiornaGiornataLega", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return result;
    }
}
